package com.bishe.shiro;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bishe.entity.User;
import com.bishe.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author passerbyYSQ
 * @create 2020-08-20 23:31
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行认证逻辑");
        UsernamePasswordToken userToken = (UsernamePasswordToken) authenticationToken;
        User one = userService.getOne(new LambdaQueryWrapper<>(User.class).eq(User::getUsername, userToken.getUsername()));

        if (one == null){
            return null; //抛出异常 UnknownAccountException
        }
        //密码认证，shiro做~
        return new SimpleAuthenticationInfo("",one.getPwd(),"");

    }
}
